package com.xbjtech.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.xbjtech.model.Role;
import com.xbjtech.model.beans.Result;
import com.xbjtech.service.RoleService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/role")
@Slf4j
public class RoleController {
	
	@Autowired
	RoleService roleService;
	
	@GetMapping("/list")
	public String list() {
		return "role/list";
	}
	
	@GetMapping("/allroles")
	@ResponseBody
	public List<Role> allRoles() {
		List<Role> roles = roleService.selectAll();
		return roles;
	}
	
	
	@GetMapping(value = "/data", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> data(
			@RequestParam(value = "page", required = false, defaultValue = "1")
			int page,
			@RequestParam(value = "rows", required = false, defaultValue = "10")
			int rows
			) {
		PageInfo<Role> roles = roleService.selectByExamplePage(page, rows, null);
		Map<String, Object> map = new HashMap<>();
		map.put("total", roles.getTotal());
		map.put("rows", roles.getList());
		return map;
	}
	
	@PostMapping("/save")
	@ResponseBody
	public Result save(@Validated Role role, Errors errors, Model model) {
		// 服务器端的校验没有做【我建议是要做服务器端的校验】
		// 
		List<FieldError> filedErros = errors.getFieldErrors();
		for (FieldError error : filedErros) {
			model.addAttribute(error.getField() + "ERROR", error.getDefaultMessage());
		}
		try {
			if (null == role.getId()) {
				roleService.insertWithoutPrimaryKey(role);
			} else {
				roleService.updateByPrimaryKeySelective(role);
			}
//			if (true)
//			throw new RuntimeException();
			// 新增成功 修改成功
			return Result.success();
		} catch (RuntimeException e) {
			// 新增失败 修改失败
			log.error("RoleController.save发生了异常=>{}", e.getMessage());
		}
		return Result.fail();
	}
	
	@GetMapping("/del")
	@ResponseBody
	public Result del(
			@RequestParam(required = true)
			Integer id) {
		try {
			roleService.deleteByPrimaryKey(id);
			return Result.success();
		} catch (RuntimeException e) {
			log.error("RoleController.del() throws exception:{}", e.getMessage());
		}
		return Result.fail();
	}
	
	@PostMapping("/saveroleandres")
	@ResponseBody
	public Result saveRoleAndRes(
			@RequestParam(value = "id", required = true)
			Integer rid,
			@RequestParam(value = "resIds", required = true)
			List<Integer> resIds
			) {
		try {
			roleService.insertRoleAndRes(rid, resIds);
			return Result.success();
		} catch (RuntimeException e) {
		}
		return Result.fail();
	}
	
	@RequestMapping("/selectResByRid")
	@ResponseBody
	public List<Integer> selectResByRid(
			@RequestParam(value = "id", required = true)
			Integer rid
			) {
		List<Integer> resIds = roleService.selectResByRid(rid);
		return resIds;
	}

}
